// 推荐菜品设置
import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:yw_bapp/dao/merchant_dao.dart';
import 'package:yw_bapp/model/menu_detail_model.dart';
import 'package:yw_bapp/widgets/filed_tag_widget.dart';
import 'package:yw_bapp/widgets/loading_pop_widget.dart';
import 'package:yw_bapp/widgets/m_btn.dart';
import 'package:yw_bapp/widgets/no_data_widget.dart';
import 'package:yw_bapp/widgets/safe_area_bar_widget.dart';
import 'package:yw_bapp/widgets/yz_img_picker.dart';

class RecommendedMenuSet extends StatefulWidget {
  final int menuId;

  const RecommendedMenuSet({Key key, this.menuId = 0}) : super(key: key);
  @override
  _RecommendedMenuSetState createState() => _RecommendedMenuSetState();
}

class _RecommendedMenuSetState extends State<RecommendedMenuSet> {
  // 下一步按钮的颜色
  bool isActive = false;
  // 按钮点击色
  Color btnActiveColor = ColorUtil.color('#FFC49226');
  // 按钮默认色
  Color btnDefaultColor = ColorUtil.color('#FFCCCCCC');
  // 文字点击色
  Color textActiveColor = Colors.white;
  // 文字默认色
  Color textDefaultColor = Colors.white;
  TextStyle actionStyle =
      TextStyle(color: Colors.black, fontSize: 15, fontWeight: FontWeight.bold);
  // 菜品标签
  List<String> _list;
  // 菜品名称
  String menuName;
  // 菜品价格
  String menuPrice;
  // 菜品图片列表
  List<String> menuImgList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _list = List<String>()..add('')..add('')..add('');
    menuImgList = List<String>()..add('');
    if (widget.menuId != 0) {
      //表示这是编辑入口进入的，需要请求详情
      _loadDetailData(context, widget.menuId);
    } else {
      LoadingPop.hide(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: XAppBar(
          statusBarColor: ColorUtil.color('#FFC49226'),
          bgColor: ColorUtil.color('#FFC49226'),
          title: "推荐菜品设置",
          iconColor: Colors.white,
        ),
        body: Container(
          padding: EdgeInsets.only(top: 10),
          color: ColorUtil.color("#FFEEEEEE"),
          child: ListView(
            children: [
              // 菜品名称
              Container(
                height: 50,
                padding: EdgeInsets.only(right: 15),
                margin: EdgeInsets.only(bottom: 10),
                color: Colors.white,
                child: Row(
                  children: [
                    FiledTagWidget(
                      tagName: '菜品名称',
                      fieldText: '$menuName',
                      hintText: '请输入菜品名称',
                      typeNum: 2,
                      changeCallback: (String value) {
                        setState(() {
                          menuName = value;
                        });
                        checkNoEmpty();
                      },
                      isEnabled: true,
                    ),
                  ],
                ),
              ),
              // 菜品价格
              Container(
                height: 50,
                padding: EdgeInsets.only(right: 15),
                margin: EdgeInsets.only(bottom: 10),
                color: Colors.white,
                child: Row(
                  children: [
                    FiledTagWidget(
                      tagName: '菜品价格',
                      fieldText: '$menuPrice',
                      hintText: '请输入菜品价格',
                      typeNum: 2,
                      changeCallback: (String value) {
                        setState(() {
                          menuPrice = value;
                        });
                        checkNoEmpty();
                      },
                      isEnabled: true,
                    ),
                    Text(
                      "元",
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 15,
                          fontWeight: FontWeight.bold),
                    )
                  ],
                ),
              ),
              // 菜品标签设置
              Container(
                height: 226,
                color: Colors.white,
                padding: EdgeInsets.fromLTRB(15, 13, 15, 14),
                child: Column(
                  children: [
                    // 标题
                    Container(
                      height: 40,
                      decoration: BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  color: ColorUtil.color("#1A000000")))),
                      child: Row(
                        children: [
                          Text(
                            "菜品标签设置",
                            style: TextStyle(color: Colors.black, fontSize: 15),
                          ),
                          Text("(最多3个标签，每个标签不超过6个字)",
                              style: TextStyle(
                                  color: ColorUtil.color("#FF999999"),
                                  fontSize: 12))
                        ],
                      ),
                    ),
                    // 标签遍历
                    Wrap(
                      direction: Axis.vertical,
                      children: _generateList(),
                    )
                  ],
                ),
              ),
              // 菜品图片设置
              Container(
                margin: EdgeInsets.only(top: 10),
                child: UcarImagePicker(
                  title: '菜品图片设置',
                  maxCount: 1,
                  onChanged: (value) {
                    print('菜品图片设置$value');
                    setState(() {
                      menuImgList[0] = value;
                    });
                    checkNoEmpty();
                  },
                ),
              ),
              // 按钮保存
              Container(
                margin: EdgeInsets.fromLTRB(25, 76, 25, 20),
                child: MBtn(
                  btnWidth: 325,
                  btnHeight: 47,
                  borderRadius: 5,
                  title: '保存',
                  hasBg: true,
                  textColor: isActive ? textActiveColor : textDefaultColor,
                  bgColor: isActive ? btnActiveColor : btnDefaultColor,
                  borderColor: isActive ? btnActiveColor : btnDefaultColor,
                  function: () => _saveMenuItem(),
                ),
              )
            ],
          ),
        ));
  }

  // 遍历标签
  List<Widget> _generateList() {
    print(_list);
    return _list
        .asMap()
        .keys
        .map((index) => tagTextFiled(index: index))
        .toList();
  }

  // 标签
  Widget tagTextFiled({int index}) {
    // 输入框控制器
    TextEditingController _controller = TextEditingController();
    _controller.text = _list[index];
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '标签${index + 1}',
          style: TextStyle(color: Colors.black, fontSize: 12),
        ),
        Container(
          width: 326,
          height: 40,
          margin: EdgeInsets.only(
              left: 20, bottom: index == 2 ? 0 : 10, top: index == 0 ? 16 : 0),
          alignment: Alignment.bottomCenter,
          decoration: BoxDecoration(
              border: Border.all(color: ColorUtil.color("#FFCCCCCC"), width: 1),
              borderRadius: BorderRadius.circular(5)),
          child: TextField(
            controller: _controller,
            keyboardType: TextInputType.text,
            autofocus: false,
            enabled: true,
            decoration: InputDecoration(
                contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 10),
                border: InputBorder.none,
                hintText: "",
                helperStyle: TextStyle(
                  color: Colors.transparent,
                ),
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey)),
                hintStyle: TextStyle(
                    color: ColorUtil.color("#FF999999"),
                    fontWeight: FontWeight.normal,
                    fontSize: 13)),
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
            textAlign: TextAlign.left,
            onChanged: (String value) {
              setState(() {
                _list[index] = value;
              });
              checkNoEmpty();
            },
          ),
        )
      ],
    );
  }

  // 保存菜品
  void _saveMenuItem() {
    // 检测数据是否为空
    // checkNoEmpty();
    //
  }

  // 检测数据是否填写完整
  bool checkNoEmpty() {
    if (menuName != '' &&
        menuPrice != '' &&
        menuImgList[0] != '' &&
        (_list[0] != '' || _list[1] != '' || _list[2] != '')) {
      setState(() {
        isActive = true;
        print(menuImgList);
      });
    } else {
      setState(() {
        isActive = false;
        print(menuImgList);
      });
    }
    print(isActive);
    return isActive;
  }

// 加载某个菜品的详情
  void _loadDetailData(BuildContext context, int menuId) {
    MerchantDao.GetMenuItemDetail(menuId).then((MenuDatailModel model) => {
          LoadingPop.hide(context),
          if (model.status == 200)
            {
              setState(() {
                menuName = model.data.dishName;
                menuPrice = model.data.dishPrice;
                menuImgList[0] = model.data.dishPic;
                model.data.dishLableArr.asMap().keys.forEach((element) {
                  _list[element] = model.data.dishLableArr[element];
                });
                print(menuName);
                print(menuPrice);
              })
            }
        });
  }
}
